/* @(#)DownloadFileVersion.java    V1.0
 *
 * Project: APEP 
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *       {CREATE_TIME} First release
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2007 Auto-Parts E-Procurement Co., Ltd. 
 *        No.251-1 Wuning Road, Shanghai, 200063, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Auto-Parts E-Procurement Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with APEP.
 *
 * Warning:
 * =============================================================================
 *   
 * 
 */

package com.apep.gw.synch;

import com.apep.gw.data.DownloadFileVersionBase;

import framework.core.Conditions;
import framework.core.DBConnection;
import framework.core.DBEngine;
import framework.core.DBResult;
import framework.core.LogWriter;
import framework.core.ObjectList;

/*
 * Author : CreateTime:{CREATE_TIME}
 */
public class DownloadFileVersion extends DownloadFileVersionBase {

    // ----system generated start----//
    private static final String SEQ_NAME = "S_DOWNLOADFILE_VERSION";

    public DownloadFileVersion() {

    }

    private static void delete(DBConnection con, String id) throws Exception {
	Conditions conditions = new Conditions();

	conditions.add(ID, id);

	delete(con, TABLE_NAME, conditions);
    }

    public static DownloadFileVersion fetch() {
	return new DownloadFileVersion();
    }

    public static DownloadFileVersion copy(DownloadFileVersion srcObj) {
	DownloadFileVersion obj = fetch();
	obj.copyBase(srcObj);
	return obj;
    }

    public static DownloadFileVersion fetch(DBConnection con, String id)
	    throws Exception {
	Conditions conditions = new Conditions();

	conditions.add(ID, id);

	return fetch(con, conditions);
    }

    public static DownloadFileVersion fetch(DBConnection con,
	    Conditions conditions) throws Exception {
	DBResult result = null;
	try {
	    result = DBEngine.doSelect(con, "select * from " + TABLE_NAME,
		    conditions);
	    java.sql.ResultSet rs = result.getRs();
	    DownloadFileVersion obj = null;
	    if (rs.next()) {
		obj = fetch(rs);
	    }
	    return obj;
	} finally {
	    if (result != null)
		result.clear();
	}
    }

    public static DownloadFileVersion fetch(java.sql.ResultSet rs)
	    throws Exception {
	DownloadFileVersion obj = new DownloadFileVersion();
	obj.initObject(rs);
	return obj;
    }

    protected void getUnique(DBConnection con) throws Exception {
	// setId(DBEngine.getId(con,SEQ_NAME));
    }

    // ----system generated end----//

    public static final String VERSION_SEP_STR = ".";
    
    
    /**
     * �õ�ָ���汾��������������а汾��Ϣ
     * 
     * @param con
     *                ��ݿ�l��
     * @param beginVersion
     *                ָ���汾�� ��ʽΪxxx.xxx.xxx
     * @return ObjectList �õ��İ汾��Ϣ�б� ���ޣ��򷵻�null
     */
    public static ObjectList getListByVersion(DBConnection con,
	    String beginVersion) throws Exception {
	int major;
	int minor;
	int reversion;

	try {
	    String[] tTempVersion = beginVersion.split(VERSION_SEP_STR);
	    major = Integer.parseInt(tTempVersion[0]);
	    minor = Integer.parseInt(tTempVersion[1]);
	    reversion = Integer.parseInt(tTempVersion[2]);
	} catch (NumberFormatException e) {
	    LogWriter.error("�汾�Ų���ϸ�ʽ[xxx.xxx.xxx]", e);
	    throw e;
	}

	return getListByVersion(con, major, minor, reversion);
    }

    /**
     * �õ�ָ���汾��������������а汾��Ϣ
     * 
     * @param con
     *                ��ݿ�l��
     * @param major
     *                ָ����汾��
     * @param minor
     *                ָ���ΰ汾��
     * @param revision
     *                ָ�����а汾��
     * @return ObjectList �õ��İ汾��Ϣ�б� ���ޣ��򷵻�null
     * @throws Exception
     */
    public static ObjectList getListByVersion(DBConnection con, int major,
	    int minor, int revision) throws Exception {
	DownloadFileVersionSearchEvent event = new DownloadFileVersionSearchEvent();
	event.setMinMajor(major);
	event.setMinMinor(minor);
	event.setMinRevision(revision);
	ObjectList list = fetch().search(con, event.toConditions());
	if(list.size() == 0)
	    return null;
	return list;
    }
    
    /**
     * ת����xml��ʽ
     * @param list
     * @return
     */
    public static String toXML(ObjectList list) throws Exception {
	StringBuffer xmlStr = new StringBuffer();
	xmlStr.append("<DownloadFileVersions>");
	for(int i=0; list != null && i<list.size(); i++) {
	    xmlStr.append(((DownloadFileVersion)list.get(i)).toXml());
	}
	xmlStr.append("</DownloadFileVersions>");
	return xmlStr.toString();
    }
}
